import http from '@/utils/request.ts'

export interface FileQueryRequest {
  pageNo?: number
  pageSize?: number
  search?: string
  fileType?: string
  minSize?: number
  maxSize?: number
  sortBy?: string
  sortOrder?: string
}

export interface FileIndexVO {
  id: string
  filePath: string
  fileName: string
  fileSize: number
  fileSizeReadable: string
  fileType: string
  directory: string
  fileModifiedTime: number
  createdTime: string
  progressPercent?: number
  currentTime?: number
  duration?: number
  // 刮削信息
  title?: string
  code?: string
  performer?: string
  cover?: string
}

export interface PageResult<T> {
  records: T[]
  total: number
  current: number
  size: number
}

export interface VideoProgressRequest {
  fileId: string
  currentTime: number
  duration: number
}

export interface VideoProgressVO {
  fileId: string
  currentTime: number
  duration: number
  progressPercent: number
}

/**
 * 文件管理 API
 */
export const fileManagerApi = {
  /**
   * 分页查询文件列表
   */
  async getFileList(params: FileQueryRequest): Promise<PageResult<FileIndexVO>> {
    // 将参数转换为查询字符串
    const queryParams = new URLSearchParams()
    Object.entries(params).forEach(([key, value]) => {
      if (value !== undefined && value !== null) {
        queryParams.append(key, String(value))
      }
    })
    
    const url = `/file/list?${queryParams.toString()}`
    const response = await http.get<PageResult<FileIndexVO>>(url)
    return response.data
  },

  /**
   * 手动扫描指定目录
   */
  async scanDirectory(directory: string): Promise<string> {
    const response = await http.post<string>(`/file/scan?directory=${encodeURIComponent(directory)}`)
    return response.data
  },

  /**
   * 扫描所有配置的目录
   */
  async scanAllDirectories(): Promise<string> {
    const response = await http.post<string>('/file/scan-all')
    return response.data
  },

  /**
   * 刷新缓存
   */
  async refreshCache(): Promise<string> {
    const response = await http.post<string>('/file/refresh-cache')
    return response.data
  },

  /**
   * 获取扫描统计信息
   */
  async getScanStats(): Promise<string> {
    const response = await http.get<string>('/file/stats')
    return response.data
  },

  /**
   * 根据ID获取文件详情
   */
  async getFileDetail(id: string): Promise<FileIndexVO> {
    const response = await http.get<FileIndexVO>(`/file/detail/${id}`)
    return response.data
  },

  /**
   * 获取视频流URL
   */
  getStreamUrl(fileId: string): string {
    return `/api/stream/video/${fileId}`
  },

  /**
   * 保存视频播放进度
   */
  async saveVideoProgress(params: VideoProgressRequest): Promise<void> {
    await http.post<void>('/file/progress/save', params)
  },

  /**
   * 随机获取一个视频文件
   */
  async getRandomVideo(): Promise<FileIndexVO> {
    const response = await http.get<FileIndexVO>('/file/random-video')
    return response.data
  },

  /**
   * 刮削所有视频文件的元数据
   */
  async scrapeAllVideos(): Promise<string> {
    const response = await http.post<string>('/file/scrape-all')
    return response.data
  },

  /**
   * 强制全量刷新
   * 重新扫描所有配置的目录，按文件修改时间从新到旧扫描
   */
  async forceFullRefresh(): Promise<string> {
    const response = await http.post<string>('/file/force-full-refresh')
    return response.data
  }
}